package com.zx.viewlearn.views;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.RelativeLayout;

import com.zx.viewlearn.R;
import com.zx.viewlearn.evaluator.PointEvaluator;
import com.zx.viewlearn.model.Point;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义太阳系view 太阳、地球、月球
 */

public class MyViewBall extends RelativeLayout {
    Paint mPaint;
    private float radius = 50;//球半径
    Point mPointEarth;//球圆心
    int mParentWidth;
    int mParentHeight;

    public MyViewBall(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();


    }

    public MyViewBall(Context context) {
        super(context);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.blue));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension((int) radius * 2, (int) radius * 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mPointEarth == null) {
            mPointEarth = new Point(radius, radius);
            drawView(canvas);
//            initRotateAnim();
        } else {
            drawView(canvas);
        }
//        super.onDraw(canvas);

    }

    public Point getmPointEarth() {
        return mPointEarth;
    }

    public void setmPointEarth(Point mPointEarth) {
        this.mPointEarth = mPointEarth;
    }
//
//    private void initRotateAnim() {
//        ViewGroup mViewGroup = (ViewGroup) getParent();
//        if (null != mViewGroup) {
//            mParentWidth = mViewGroup.getWidth();
//            mParentHeight = mViewGroup.getHeight();
//        }
//        Log.e("MyViewBall", "mParentWidth:" + mParentWidth);
//        Log.e("MyViewBall", "mParentHeight:" + mParentHeight);
//        Point startPoint = new Point(mParentWidth / 2, radius);
//        Point endPoint = new Point(mParentWidth / 2, mParentHeight - radius);
//        ValueAnimator mRotateAnim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
//        mRotateAnim.setInterpolator(new BounceInterpolator());//设置插值器
//        mRotateAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                mPointSun = (Point) animation.getAnimatedValue();
//                invalidate();
//            }
//        });
//        //计算小球旋转的中心点(此点的左边是在小球自身的坐标系中)
//        mRotateAnim.setDuration(3000);
////        mRotateAnim.setInterpolator(getInterpolator());
////        mRotateAnim.setRepeatCount(-1);
////        mRotateAnim.setStartDelay(500);
//        mRotateAnim.start();
//    }


    private void drawView(Canvas canvas) {
//        Log.e("MyViewBall", "getWidth:" + getWidth());
//        Log.e("MyViewBall", "getHeight:" + getHeight());
//        Log.e("MyViewBall", "mPointSun.getX():" + mPointSun.getX());
//        Log.e("MyViewBall", "mPointSun.getY():" + mPointSun.getY());
        canvas.drawCircle(mPointEarth.getX(), mPointEarth.getY(), radius, mPaint);

    }


    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }
}
